<?php

namespace App\Admin\Controllers;

use App\Department;
use App\Handbook;
use App\Type;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class HandbookController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文档';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Handbook);
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView(); // 去掉查看
        });
        $grid->model()->orderBy('id','desc');

        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
        ];

        $grid->column('id', __('Id'))->sortable();
        $grid->column('img_url', __('封面图'))->display(function ($img){
            return "<p><img src='".$img."' alt='' style='width: 60px;opacity:0.8;'></p>";
        });
        $grid->column('title', __('文档标题'))->display(function ($title){
            return str_limit($title,100,'...');
        });
        $grid->column('depart.department_name', __('所属部门'))->width(120);
        $grid->column('type.type_name', __('文档类型'))->width(120);
        $grid->column('sort_num', __('排序'))->editable()->sortable()->width(80);
        $grid->column('state','是否展示')->switch($states)->width(80);
        $grid->column('created_at', __('创建时间'))->width(150);
        $grid->column('updated_at', __('更新时间'))->width(150);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Handbook::findOrFail($id));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Handbook);
        $de_list = Department::where('id','>',0)->pluck('department_name', 'id')->toArray();
        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
        ];

        $form->text('title', __('文档标题'));
        $form->textarea('desc_con', '简介')->rows(3)->default('能力配不上野心，是所有烦扰的根源。这个世界是公平的，你要想得到，就得学会付出和坚持。每个人都是通过自己的努力，去决定生活的样子。');
        $form->text('img_url', __('封面图'))->default('http://nav.liziyun.net/imgs/help_book.jpg');
        $form->select('department_id', '所属部门')->options($de_list)->load('type_id', '/admin/type');
        $form->select('type_id', __('文档类型'))->options(function($id){
            return Type::where('id' , $id)->pluck('type_name' , 'id');
        });
        $form->number('sort_num',__('排序'))->default(1);
        $form->switch('state',__('是否展示'))->states($states)->default(1);
        $form->editor('contents', '内容');

        return $form;
    }

    public function type(){
        $depart_id = $_GET['q'];
        $list = Type::where('depart_id',$depart_id)->select('id','type_name as text')->get()->toArray();
        return $list;
    }
}
